﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Controller;
using Temp.Model;
using Temp.View;

namespace Temp.Planner2
{
    class StoryPlanner
    {

        public int MAX_CAPACITY_INVENTORY = 10;

        private StoryNodeController snc;
        private PeerController pc;
        private OntologyController oc;
        private List<string> activityLog;
        private List<Item> listInventory;
        public StoryNode currentStoryNode { get; set; }
        public EventPlanner eventPlanner { get; set; }
        public int gameTime { get; set; }
        private int elapsedTime;
        public DialogueView dv { get; set; }


        public StoryPlanner(List<string> activityLog, List<Item> listInventory)
        {
            this.activityLog = activityLog;
            this.listInventory = listInventory;
            snc = new StoryNodeController();
            pc = new PeerController();
            oc = new OntologyController();
            currentStoryNode = snc.getStoryNode(1);
            //currentStoryNode = snc.getStoryNode(17);
            //activityLog.Add("tic tac toe");
            //activityLog.Add("eat burger");
            this.dv = dv;
            elapsedTime = 0;
            //init currentStoryNode
            activityLog.Add("instructions");
            activityLog.Add("news");
        }

        public void initEventPlanner() {
            eventPlanner.Initialize(currentStoryNode.name, currentStoryNode.name, "STORY NODE");  
        }

        public void reEvaluateStory()
        {
            List<Ontology> childNodes = snc.getChildNodes(currentStoryNode.name);
            foreach (Ontology child in childNodes)
            {
                List<Ontology> criteriaPerChild = snc.getStoryCriterias(child.concept2);
                System.Diagnostics.Debug.WriteLine("child name:" + child.concept2 + " criteria count:" + criteriaPerChild.Count);  

                if (compareCriteriaToActivity(criteriaPerChild))
                {
                    changeCurrentStoryNode(child);
                    /*if (currentStoryNode.forPeer != 0) { 
                        List<Conversation> tempList = new List<Conversation>();
                        Conversation c = new Conversation("Alex", pc.getDialogue(currentStoryNode.forPeer), 'I');
                        tempList.Add(c);
                        dv.listDialogue = tempList;
                        dv.modifyListDialogue();
                        dv.isShowing = true;
                    }*/

                    eventPlanner.Initialize(currentStoryNode.name, currentStoryNode.name, "STORY NODE");
                    reEvaluateStory();
                }
                
            }

        }

        private void changeCurrentStoryNode(Ontology child)
        {
            currentStoryNode = snc.getStoryNode(child);
            System.Diagnostics.Debug.WriteLine("NEW CHILD:" + currentStoryNode.name);
            //reEvaluateStory();
        }

       
        private bool compareCriteriaToActivity(List<Ontology> criteriaPerChild)
        {
            //System.Diagnostics.Debug.WriteLine("==================================================" );
            foreach (Ontology c in criteriaPerChild) {
                //System.Diagnostics.Debug.WriteLine("criteria " + c.concept2);
            }
           // System.Diagnostics.Debug.WriteLine("==================================================");

            //System.Diagnostics.Debug.WriteLine("=====================activity log=============================");
            foreach (string s in activityLog)
            {
                //System.Diagnostics.Debug.WriteLine("activity " + s);
            }
            //System.Diagnostics.Debug.WriteLine("=====================activity log=============================");
            //System.Diagnostics.Debug.WriteLine("SIZE: " + criteriaPerChild.Count);
            int tempSize = criteriaPerChild.Count;
            for (int i = 0; i < tempSize; i++)
            {
                Ontology criteria = criteriaPerChild[0];
               
               // System.Diagnostics.Debug.WriteLine("criteria " + criteria.concept2);
                if (criteria.concept2.Contains("_MIN"))
                {
                    int time = int.Parse(criteria.concept2.Replace("_MIN", ""));
                    System.Diagnostics.Debug.WriteLine("gametime" + gameTime + "elapse time" + elapsedTime);
                    if (time == gameTime - elapsedTime)
                    {
                        System.Diagnostics.Debug.WriteLine("new child in time part");
                        elapsedTime = gameTime;
                        return true;
                    }

                }
                
                else if (criteria.concept2.Contains("INVENTORY")) {
                    System.Diagnostics.Debug.WriteLine("IN INVENTORY");
                    if (criteria.concept2 == "INVENTORY_FULL")
                    {
                        if (listInventory.Count >= MAX_CAPACITY_INVENTORY)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        string[] a = criteria.concept2.Split('_');
                        int number = Convert.ToInt16(a[1]);
                        string ontTable = a[2].ToLower() + "_ont";
                        string relation = a[3].ToLower();
                        string concept = a[4].ToLower();
                        List<Ontology> result = oc.genericQuery(ontTable, relation, concept);

                        System.Diagnostics.Debug.WriteLine("LIST INVENTORY SIZE : " + listInventory.Count);

                        System.Diagnostics.Debug.WriteLine("result : " + result.Count);
                        int ctr = 0;
                        foreach (Item it in listInventory)
                        {
                            for (int b = 0; b < result.Count; b++)
                            {
                                System.Diagnostics.Debug.WriteLine(it.name.ToLower() + "==" + result[b].concept1.ToLower());
                                if (result[b].concept1.ToLower() == it.name.ToLower())
                                {
                                    ctr++;
                                    if (ctr == number)
                                    {
                                        System.Diagnostics.Debug.WriteLine("RETURN TRUE");
                                        return true;
                                    }
                                    else{
                                        System.Diagnostics.Debug.WriteLine("CTR:" + ctr);
                                    }
                                }
                            }
                        }
                        System.Diagnostics.Debug.WriteLine("ctr:" + ctr);
                        return false;
                    }
                }

                else
                {
                    //System.Diagnostics.Debug.WriteLine("==========================================");
                    foreach (string s in activityLog)
                    {
                        //System.Diagnostics.Debug.WriteLine("s: " + s + " concept2:" + criteria.concept2);
                        if (s.Equals(criteria.concept2))
                        {
                            //System.Diagnostics.Debug.WriteLine("same: " + s);

                            criteriaPerChild.Remove(criteria);
                        }

                        //if (s.equals("keep candles") || s.equals("keep food") || s.equals("keep phone") || s.equals("keep radio"))
                        //{
                        //    system.diagnostics.debug.writeline("s: " + s + " concept2:" + criteria.concept2);
                        //}

                    }
                    //System.Diagnostics.Debug.WriteLine("==========================================");
                }
            }
            //System.Diagnostics.Debug.WriteLine("SIZE: " + criteriaPerChild.Count);
            if (criteriaPerChild.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("CRITERIA PER CHILD == 0 ");
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
